<?php

class Core_Plugin_System extends Zend_Controller_Plugin_Abstract
{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        //Get params
        $params = $request->getParams();

        //Get redirect helper
        $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');

        //User data
        $data = Core_Session::get('user');

        if (!$data && ($params['module'] != 'default' || $params['controller'] != 'index' || $params['action'] != 'index'))
        {
            if (!$request->isPost() || ($params['module'] != 'default' || $params['controller'] != 'index' || $params['action'] != 'register'))
            {
                $redirector->gotoUrl('index');
            }            
        }

        if ($params['module'] != 'default' || $params['controller'] != 'index' || $params['action'] != 'logout')
        {
            switch ($data['Type'])
            {
                case "1":

                    if ($params['module'] != 'reader')
                    {
                        $redirector->gotoUrl('reader');
                    }

                    break;
                case "2":

                    if ($params['module'] != 'librarian')
                    {
                        $redirector->gotoUrl('librarian');
                    }

                    break;
                default:
                    break;
            }
        }

        if ($data['Type'])
        {
            /**
             * Init menu 
             */
            $menu = new Zend_Config(require APPLICATION_PATH . '/configs/menu.php');
            Zend_Layout::getMvcInstance()->getView()->assign('menu', $menu->get($data['Type'])->toArray());
        }                
    }

}

